﻿using DAL.DBHelper;
using Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DAL
{
    public class MQ_CLASSIFICATIONService
    {
        private static string connectionString = ConfigurationManager.ConnectionStrings["connString"].ToString();
        /// <summary>
        /// 获取表的全部数据列表
        /// </summary>
        /// <returns>返回全部数据列表</returns>
        public List<MQ_CLASSIFICATION> mQ_CLASSIFICATIONs()
        {
            string sql = "SELECT CF_ID, CF_CODE, CF_NAME, CF_PARENT_ID, CF_ELECTRON, CF_SAOMIAO, CF_SORT, CF_IS_ENABLED, CF_INPUT_CODE FROM [dbo].[MQ_CLASSIFICATION]";
            List<MQ_CLASSIFICATION> mQ_List = new List<MQ_CLASSIFICATION>();
            SqlDataReader objReader = SQLHelper.GetReader(sql);
            while (objReader.Read())
            {
                mQ_List.Add(new MQ_CLASSIFICATION(Convert.ToInt32(objReader["CF_ID"]), objReader["Cf_CODE"].ToString(), objReader["Cf_NAME"].ToString(), objReader["CF_PARENT_ID"].ToString(), Convert.ToInt32(objReader["CF_SORT"]), Convert.ToInt32(objReader["CF_IS_ENABLED"]), objReader["CF_INPUT_CODE"].ToString()));
            }
            objReader.Close();
            return mQ_List;
        }

        /// <summary>
        /// 获取单个分类对象
        /// </summary>
        /// <param name="cfCode">传入CODE</param>
        /// <returns>返回对象</returns>
        public static MQ_CLASSIFICATION GetClassificationById(string cfCode)
        {
            if (cfCode == null)
            {
                //MessageBox.Show("输入数据无效");
            }
            else
            {
                using (var connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    string sql = "SELECT * FROM MQ_CLASSIFICATION WHERE Cf_CODE = @CfCode";

                    using (var command = new SqlCommand(sql, connection))
                    {
                        command.Parameters.AddWithValue("@CfCode", cfCode);

                        using (var reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                return new MQ_CLASSIFICATION(Convert.ToInt32(reader["CF_ID"]), reader["Cf_CODE"].ToString(), reader["Cf_NAME"].ToString(), reader["CF_PARENT_ID"].ToString(), Convert.ToInt32(reader["CF_SORT"]), Convert.ToInt32(reader["CF_IS_ENABLED"]), reader["CF_INPUT_CODE"].ToString());
                            }
                        }
                    }

                }
            }
            return null;
        }

        /// <summary>
        /// 根据name获取CODE
        /// </summary>
        /// <param name="name">传入name</param>
        /// <returns>返回CODE</returns>
        public static string Name_Obj(string name)
        {
            //sql
            string sql = $"SELECT  CF_CODE FROM [dbo].[MQ_CLASSIFICATION] where CF_NAME = @name";

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();

                using (var command = new SqlCommand(sql, connection))
                {
                    command.Parameters.AddWithValue("@name", name);
                    object result = command.ExecuteScalar();
                    return result != null ? result.ToString() : null;

                }
            }

        }

        /// <summary>
        /// 根据CF_PARENT_ID获取CODE
        /// </summary>
        /// <param name="ID">传入CF_PARENT_ID</param>
        /// <returns>返回CODE</returns>
        public static string CF_PARENT_ID_Obj(string ID)
        {
            if(Convert.ToInt32(ID) == 0)
            {
                return null;
            }
            else
            {
                //sql
                string sql = $"SELECT CF_CODE FROM [dbo].[MQ_CLASSIFICATION] where CF_PARENT_ID = @ID";

                using (var connection = new SqlConnection(connectionString))
                {
                    connection.Open();

                    using (var command = new SqlCommand(sql, connection))
                    {
                        command.Parameters.AddWithValue("@ID", ID);
                        object result = command.ExecuteScalar();
                        return result != null ? result.ToString() : null;
                    }
                }
            }
           
        }

        /// <summary>
        /// 递归获取所有父级对象
        /// </summary>
        /// <param name="startCfCode">传入CODE</param>
        /// <returns>返回所有父子级列表</returns>
        public static List<MQ_CLASSIFICATION> GetAllParents(string startCfCode)
        {
            var result = new List<MQ_CLASSIFICATION>();
            GetParentsRecursive(startCfCode, result);
            return result;
        }

        private static void GetParentsRecursive(string cfCode, List<MQ_CLASSIFICATION> parents)
        {
            var current = GetClassificationById(cfCode);
            if (current == null) return;

            // 添加当前对象到列表
            parents.Add(current);

            // 如果有父级ID，继续递归
            if (!string.IsNullOrEmpty(current.CF_PARENT_ID))
            {
                GetParentsRecursive(current.CF_PARENT_ID, parents);
            }
        }
    }
}
